package cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemDictionaryQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;

import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.Request;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    ISystemDictionaryService systemDictionaryService;

    @Request(name="字典细明页面",expression="systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQueryObject qo){
        /*PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);*/
       /* model.addAttribute("pageResult", pageInfo);*/
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("systemDictionaries",systemDictionaries);
        PageInfo<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.query(qo);
        model.addAttribute("pageResult", systemDictionaryItems);
            return "systemDictionaryItem/list";
    }


    @Request(name="字典细明删除",expression="systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @Request(name="字典细明新增/编辑",expression="systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) {
            systemDictionaryItemService.save(systemDictionaryItem);
        } else {
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @Request(name="字典细明新增/编辑",expression="systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(SystemDictionaryItem systemDictionaryItem, Model model) {
        systemDictionaryItemService.save(systemDictionaryItem);
        return "systemDictionaryItem/input";
    }

    @RequestMapping("/selectSeyetem")
    @ResponseBody
    List<SystemDictionaryItem> selectSystem(SystemDictionaryQueryObject qo){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.checkPatentId(qo);
        System.out.println(qo.getTypeId());
        Long parentId = qo.getParentId();
        return systemDictionaryItems;
    }

    @RequestMapping("/selectSeyetem1")
    @ResponseBody
    List<SystemDictionaryItem> selectSystem1(SystemDictionaryQueryObject qo){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.checkPatentId2(qo);
        System.out.println(qo.getTypeId());
        System.out.println(qo.getParentId()+"========================================");
        return systemDictionaryItems;
    }

    @Request(name="字典细明编辑",expression="systemDictionaryItem:update")
    @RequestMapping("/update")
    public String update(SystemDictionaryItem systemDictionaryItem , Model model) {
        if (systemDictionaryItem.getId() != null) {
            model.addAttribute("systemDictionaryItem", systemDictionaryItemService.get(systemDictionaryItem.getId()));
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }
}
